//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.eResearch.Blackboard.Wcf
{
    using System;
    using System.ServiceModel;
    using Microsoft.Research.eResearch.Blackboard.Common;

    /// <summary>
    /// Provides an extension that the WCF ServiceHost can use to expose the single Blackboard instance to 
    /// multiple instances of the <see cref="WcfNotificationSubscriptionService"/> service. 
    /// </summary>
    public class NotificationSubscriptionReceiverExtension : IExtension<ServiceHostBase>
    {
        /// <summary>
        /// Method to be executed whenever subscription is performed.
        /// </summary>
        private Action<Uri, SubscriptionProfile> onSubscribe;

        /// <summary>
        /// Method to be executed whenever un-subscribe is performed.
        /// </summary>
        private Action<Uri> onUnSubscribe;

        /// <summary>
        /// Initializes a new instance of the <see cref="NotificationSubscriptionReceiverExtension"/> class.
        /// </summary>
        /// <param name="onSubscribe">A delegate specifying the method to call when the Subscribe method is called.</param>
        /// <param name="onUnsubscribe">A delegate specifying the method to call when the Unsubscribe method is called.</param>
        public NotificationSubscriptionReceiverExtension(Action<Uri, SubscriptionProfile> onSubscribe, Action<Uri> onUnsubscribe)
        {
            if (onSubscribe == null)
            {
                throw new ArgumentNullException("onSubscribe");
            }

            if (onUnsubscribe == null)
            {
                throw new ArgumentNullException("onUnsubscribe");
            }

            this.onSubscribe = onSubscribe;
            this.onUnSubscribe = onUnsubscribe;
        } 

        /// <summary>
        /// Enables an extension object to find out when it has been aggregated. 
        /// Called when the extension is added to the <see cref="P:System.ServiceModel.IExtensibleObject`1.Extensions"/> property.
        /// </summary>
        /// <param name="owner">The extensible object that aggregates this extension.</param>
        public void Attach(ServiceHostBase owner)
        {
        } 

        /// <summary>
        /// Enables an object to find out when it is no longer aggregated. Called when an extension is 
        /// removed from the <see cref="P:System.ServiceModel.IExtensibleObject`1.Extensions"/> property.
        /// </summary>
        /// <param name="owner">The extensible object that aggregates this extension.</param>
        public void Detach(ServiceHostBase owner)
        {
        } 

        /// <summary>
        /// Subscribes the specified return address with the specified subscription profile.
        /// </summary>
        /// <param name="returnAddress">The return address.</param>
        /// <param name="profile">The profile.</param>
        public void Subscribe(Uri returnAddress, SubscriptionProfile profile)
        {
            this.onSubscribe(returnAddress, profile);
        } 

        /// <summary>
        /// Unsubscribes the specified return address.
        /// </summary>
        /// <param name="returnAddress">The return address.</param>
        public void Unsubscribe(Uri returnAddress)
        {
            this.onUnSubscribe(returnAddress);
        } 
    }
}
